package com.xinqi.modules.prepare.data.manager.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.current.Currents;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.prepare.data.convert.PrepareDataConvert;
import com.xinqi.modules.prepare.data.domain.PrepareDataEntity;
import com.xinqi.modules.prepare.data.dto.req.PrepareDataQueryDTO;
import com.xinqi.modules.prepare.data.dto.rsp.PrepareDataResultDTO;
import com.xinqi.modules.prepare.data.manager.PrepareDataManager;
import com.xinqi.modules.prepare.data.mapper.PrepareDataMapper;
import com.xinqi.modules.prepare.folder.domain.PrepareFileFolderEntity;
import com.xinqi.modules.prepare.prepare.enums.PrepareOperateLogEnum;
import com.xinqi.modules.prepare.prepare.manager.PrepareOperateLogManager;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 备课区资料管理层实现
 *
 * @author: system
 * @date: 2023/03/19
 */
@Service
@RequiredArgsConstructor
public class PrepareDataManagerImpl extends BaseServiceImpl<PrepareDataMapper, PrepareDataEntity> implements PrepareDataManager {
    private final PrepareDataConvert prepareDataConvert;
    private final PrepareOperateLogManager prepareOperateLogManager;
    /**
     * 自定义分页查询
     */
    @Override
    public Page<PrepareDataResultDTO> findPage(Page<Object> page, PrepareDataQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<PrepareDataResultDTO> findList(PrepareDataQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 备课区资料数据
     */
    @Override
    public PrepareDataEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 备课区资料数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        return deleteById(findById(id));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copy(Long id, PrepareFileFolderEntity target) {
        PrepareDataEntity entity = findById(id);
        PrepareDataEntity copy = prepareDataConvert.copy(entity, new PrepareDataEntity());
        copy.setUserId(Currents.userId());
        copy.setPrepareId(target.getPrepareId());
        copy.setName(target.getName());
        save(copy);
        target.setRefId(copy.getId());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void move(PrepareFileFolderEntity file) {
        PrepareDataEntity entity = findById(file.getRefId());
        entity.setPrepareId(file.getPrepareId());
        entity.setName(file.getName());
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean restore(Long id) {
        return this.baseMapper.restoreById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(PrepareDataEntity entity) {
        if (entity == null) {
            return false;
        }
        prepareOperateLogManager.addFileOperateLog(entity, PrepareOperateLogEnum.FILE_DEL);
        return removeById(entity.getId());
    }
}
